package com.muhuahealth.service.impl;

import com.muhuahealth.dao.UndergroundPipeDao;
import com.muhuahealth.entity.UndergroundPipeEntity;
import com.muhuahealth.service.UndergroundPipeService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/1/24 17:33
 */
@Service
public class UndergroundServiceImpl implements UndergroundPipeService {

    @Autowired
    UndergroundPipeDao undergroundPipeDao;

    @Override
    public PageResult getPipeListPage(PageQueryUtil queryUtil) {

        //展示分页内容
        List<UndergroundPipeEntity> pipeEntityList = undergroundPipeDao.findPipeListPage(queryUtil);
        //计算总页数
        int totalPage = undergroundPipeDao.getTotalPage(queryUtil);
        //封装分页
        PageResult result = new PageResult(pipeEntityList,totalPage,queryUtil.getLimit(),queryUtil.getPage());
        return result;
    }

    @Override
    public ServerResponse savePipeInfo(UndergroundPipeEntity undergroundPipeEntity) {

        int insertInfo = undergroundPipeDao.savePipeInfo(undergroundPipeEntity);
        if(insertInfo > 0){
            return ServerResponse.createSuccess("插入成功！");
        }
        return ServerResponse.createError("插入失败！");
    }

    @Override
    public ServerResponse updatePipeInfo(UndergroundPipeEntity undergroundPipeEntity) {
        //判断是否存在待更新的数据
        UndergroundPipeEntity pipeEntity = undergroundPipeDao.selectByPrimaryKey(undergroundPipeEntity.getId());
        if(pipeEntity == null){
            return ServerResponse.createError("data is not exist!");
        }
        //判断待更新的数据是否大于0
        int updateInfo = undergroundPipeDao.updatePipeInfo(undergroundPipeEntity);
        if(updateInfo > 0){
            return ServerResponse.createSuccess("更新成功！");
        }
        return ServerResponse.createError("更新失败！");
    }

    @Override
    public boolean deleteById(Integer id) {

        if(undergroundPipeDao.deleteById(id)>0){
            return true;
        }
        return false;
    }

    @Override
    public ServerResponse searchPipeInfo(UndergroundPipeEntity undergroundPipeEntity) {

        return undergroundPipeDao.searchPipeInfo(undergroundPipeEntity);
    }
}
